package com.chixing.controller;


import com.aliyuncs.utils.StringUtils;
import com.chixing.commons.MD5Util;
import com.chixing.commons.ServerResponse;
import com.chixing.pojo.CompanyManage;
import com.chixing.pojo.PersonCenter;
import com.chixing.service.IPersonCenterService;
import com.chixing.ws.ChatEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 个人中心表 前端控制器
 * </p>
 *
 * @author Jason Marslyon
 * @since 2022-10-04
 */
//@Controller
//@RequestMapping("/personCenter")
@RestController
public class RegisterLoginController {
    @Autowired
    public IPersonCenterService personCenterService;
    @Autowired
    public RedisTemplate<String,String> redisTemplate;


    @PostMapping("personCenter/save")
    @ResponseBody
    public ServerResponse save(PersonCenter personCenter){
        System.out.println("zzzzzzzzzzzz"+personCenter);
        personCenter.setPersonStatic(0);
        personCenter.setPersonAge(22);
        personCenter.setPersonName("我是一颗小虎牙");
        ServerResponse result =personCenterService.save(personCenter);
        return result;
    }

//    ==================== 登录验证请求跳转？ ===========================
//    @PostMapping("checkLogin")
//    @ResponseBody
//    public Result checkLogin(Long userLoginTel, String userLoginPassword, HttpSession session){
//        PersonCenter personCenter = personCenterService.userCheckLogin(userLoginTel,userLoginPassword);
//        if(personCenter!=null) {
//            session.setAttribute("personCenter", personCenter);
//            return new Result(11111, "登录成功", personCenter);
//        }
//        return new Result(00000,"登录失败",personCenter);
//    }



    //    登录token
    @PostMapping("checkLoginToken")
    public ServerResponse login(PersonCenter personCenter, HttpServletRequest request, HttpServletResponse response,HttpSession session){
        //  将页面获取的password进行加密再在数据库中查
        String loginPassoword = personCenter.getPersonPassword();
        System.out.println(loginPassoword+"rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
        String md5password = MD5Util.md5PassWord(loginPassoword);

        personCenter.setPersonPassword(md5password);
        System.out.println(personCenter.getPersonTel()+personCenter.getPersonPassword()+"rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
//        MySql验证前端输入的用户号码和密码
        ServerResponse result = personCenterService.loginToken(personCenter);
        PersonCenter checkPersonCenter = personCenterService.checkLogin(personCenter);
        if (checkPersonCenter != null) {
            session.setAttribute("loginPerson", checkPersonCenter);
        }
        if (result.getResultCode() == 200){
            String token = (String) result.getData();
            System.out.println("personController用户登录成功>>>"+result);
        }
        return result;
    }

    //验证码注册
    @PostMapping("/registerSend")
    public ServerResponse registerSerSendCode(@RequestParam("phone")String phone){
        System.out.println("controller:"+phone);
        //根据手机号从redis中拿验证码
        String code =redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)){
            return ServerResponse.success("发送成功",code);
        }
        // 如果redis 中根据手机号拿不到验证码，则生成6位随机验证码
        code =String.valueOf(UUID.randomUUID().toString().hashCode()).replace("-","").substring(0,6);
        System.out.println("code" + code);
        //验证码存入codeMap
        Map<String,Object>codeMap =new HashMap<>();
        codeMap.put("code",code);
        //调用aliyunSendSmsService发送短信
        Boolean bool =personCenterService.sendMessage(phone,code,codeMap);
        System.out.println(code);
        if (bool) {
            redisTemplate.opsForValue().set(phone,code,50000, TimeUnit.MINUTES);
            return ServerResponse.success("发送成功",code);
        }else {
            return ServerResponse.fail("发送失败",null);
        }
    }







    //消息聊天框部分

    @RequestMapping("personCenter/getUsername")
    public PersonCenter getUsername(HttpSession session) {
        PersonCenter personCenter = (PersonCenter) session.getAttribute("loginPerson");
        return personCenter;
    }

    @RequestMapping("personCenter/getAllUsername")
    public ServerResponse getAllUsername(HttpServletRequest httpServletRequest) {
        String token = httpServletRequest.getHeader("token");
        List<String> allUsername = new ArrayList<>();
        System.out.println(token == null);
        System.out.println(token);
        if(token.equals("null")){
            //高管登录
            allUsername = personCenterService.getAllUsername();
        }else {
            //用户登录
            allUsername = personCenterService.getAllManageName();
        }
        return ServerResponse.success("成功", allUsername);
    }

    @RequestMapping("personCenter/isOnline")
    public ServerResponse isOnline() {
        Map<String, ChatEndpoint> onlineUsersName = ChatEndpoint.onlineUsersName;
        Set<String> names = onlineUsersName.keySet();
        List<String> allOnlineName = new ArrayList<>();
        allOnlineName.addAll(names);
        return ServerResponse.success("成功", allOnlineName);
    }

}
